#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main()
{
    int fd = open("log.txt", O_RDONLY);
    if(fd < 0) return 1;

    printf("fd -> %d\n", fd);
    struct stat st = { 0 };
    fstat(fd, &st);
    printf("file size->%zd\n", st.st_size);

    //多开一个字节的空间用来存放字符'\0'：
    char* buf = (char*)malloc(st.st_size + 1);
    //int ret = read(fd, buf, st.st_size);
    int ret = read(fd, buf, 12);
    if(ret >= 0)
    {
        printf("buf size->%d\n", ret);
        buf[ret] = '\0';
        printf("%s", buf);
    }

    free(buf);
    //关闭文件：
    close(fd);
    return 0;
}
